package com.etl.hadoop.util;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;

public class Record implements WritableComparable {
	String recordString;
	List<String> recList;

	public Record() {
		recordString = "";
	}

	public String getRecordString() {
		return recordString;
	}

	public Record(String rec) {
		recordString = rec;
		setRecList(rec);

	}

	private void setRecList(String rec) {
		rec.split(",");
		recList = Arrays.asList(rec.split(","));
	}

	public Record(ArrayList<String> rec) {
		if (rec == null || rec.size() <= 0) {
			return;
		}
		StringBuffer r = new StringBuffer();
		r.append(rec.get(0)).append(",");
		for (int i = 1; i < rec.size(); i++) {
			r.append("," + rec.get(i));
		}
		recordString = r.toString();
		setRecList(recordString);
	}

	public String getRec(int i) {
		if (recList == null) {
			return null;
		}
		if (i < 0 || i > recList.size()) {
			return null;
		}
		return recList.get(i);
	}

	public String toString() {
		return recordString;
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		// TODO Auto-generated method stub
		setRecList(in.readLine());
	}

	@Override
	public void write(DataOutput out) throws IOException {
		out.writeChars(recordString);
	}

	@Override
	public int compareTo(Object crec) {
		// TODO Auto-generated method stub
		return this.getRecordString().compareTo(
				((Record) crec).getRecordString());

	}

	public int hashCode() {
		return this.getRecordString().length();
	}

}
